Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

129
Vistas
Splitting a string like "__f__g_f_" Javascript

Say I have a string __f__g_f_

How can I achieve splitting it up to an array like ['__','f','__','g','_','f','_']

almost 4 years ago · Santiago Trujillo
3 Respuestas
Responde la pregunta

0

This should do:

const str = '__f__g_f_'

const parts = str.split(/([a-z]+)/)

console.log(parts)

almost 4 years ago · Santiago Trujillo Denunciar

0

Instead of using .split() you can use .match() with a matching group in the regex, it will give you an array of all your wanted matches.

const str = '__f__g_f_'
const parts = str.match(/(_+)|([a-z]+)/g)
console.log(parts);
[
  "__",
  "f",
  "__",
  "g",
  "_",
  "f",
  "_"
]

almost 4 years ago · Santiago Trujillo Denunciar

0

Split works at O(N) time complexity so you can write your own implementation and it is more readable and more maintainable

const s = "__f__g_f_"
const array = []

let buffer = ""
for (let i = 0; i < s.length; i++) {

    if (s[i] === "_") {
        buffer += s[i]
        continue
    }

    if (buffer.length > 0) {
        array.push(buffer)
        buffer = ""
    }
    array.push(s[i])
}
if (buffer.length > 0) {
    array.push(buffer)
}
console.log(array)

Edit:

(/([a-z]+)/) is elegant but always be in the shoes of other developers. What does (/([a-z]+)/) mean at a glance ? Regex is like a compiled program, machine code. unless all developers are adept in regex, They are not self-documented. They require to be commented on. And there might have bugs in them, how can you trust /([a-z]+)/ if it actually works without running the code? Seeing human readable code is always preferable. And finally, you may be surprised that regex can have weird time complexity, sometimes they jump to exponential. You don't know how they are implemented. Here, a simple plain and readable implementation is 100% faster than the regex.

console.time('regex');
for (let i = 0; i < 100_000_000; i++) {
  const str = '__f__g_f_'
  const parts = str.split(/([a-z]+)/)
}
console.timeEnd("regex")


console.time('plain_fast');

for (let i = 0; i < 100_000_000; i++) {
  const s = "__f__g_f_"
  const array = []

  let buffer = ""
  for (let i = 0; i < s.length; i++) {

    if (s[i] === "_") {
      buffer += s[i]
      continue
    }

    if (buffer.length > 0) {
      array.push(buffer)
      buffer = ""
    }
    array.push(s[i])
  }
  if (buffer.length > 0) {
    array.push(buffer)
  }
}
console.timeEnd("plain_fast")

regex: 10540.700ms
plain_fast: 4556.500ms
almost 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda